# config valid for current version and patch releases of Capistrano
lock "~> 3.16.0"

set :pty, true
set :ssh_options, forward_agent: true

set :application, "pipup-lms-be"
set :repo_url, 'git@gitlab.pipup.me:pipup/pipup-lms-be.git'
set :keep_releases, 30


set :linked_files, fetch(:linked_files, []).push('.env')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets')
set :sidekiq_config, -> { "#{current_path}/config/sidekiq.yml" }
set :whenever_identifier, -> { "#{fetch(:application)}_#{fetch(:stage)}" }

after 'deploy:publishing', 'puma:restart'

namespace :rails do
  desc "Remote console"
  task :console do
    on roles(:app) do |host|
      execute_interactively "/usr/local/rvm/bin/rvm default do bundle exec rails console #{fetch(:rails_env)}", host.user
    end
  end

  desc "server log"
  task :log do
    on roles(:app) do |host|
      execute_interactively "tail -f log/*.log", host.user
    end
  end

  desc "Remote dbconsole"
  task :dbconsole do
    on roles(:app) do |host|
      execute_interactively "/usr/local/rvm/bin/rvm default do bundle exec rails dbconsole #{fetch(:rails_env)}", host.user
    end
  end

  def execute_interactively(command, user)
    info "Connecting with #{user}@#{host}"
    cmd = "ssh #{user}@#{host} -p 22 -t 'cd #{fetch(:deploy_to)}/current && #{command}'"
    exec cmd
  end
end
